<?php

include '../includes/common.inc.php';
include $main_path . 'language/' . $language . '/categories.inc.php';
include $include_path . 'dates.inc.php';

//$query = "SELECT * FROM webid_auctions WHERE title like '%".$_GET["value"]."%'";
$NOW = time();
$cat_id = intval($_POST['id_cat']);
$catSQL = '';
	if ($cat_id > 0)
	{
		$catscontrol = new MPTTcategories();
		$query = "SELECT right_id, left_id FROM " . $DBPrefix . "categories WHERE cat_id = " . $cat_id;
		$res = mysql_query($query);
		$system->check_mysql($res, $query, __LINE__, __FILE__);
		$parent_node = mysql_fetch_assoc($res);
		$children = $catscontrol->get_children_list($parent_node['left_id'], $parent_node['right_id']);
		$childarray = array($cat_id);
		foreach ($children as $k => $v)
		{
			$childarray[] = $v['cat_id'];
		}
		$catalist = '(';
		$catalist .= implode(',', $childarray);
		$catalist .= ')';
		$catSQL = " AND (category IN " . $catalist;
		if ($system->SETTINGS['extra_cat'] == 'y')
		{
			$catSQL .= " OR secondcat IN " . $catalist;
		}
		$catSQL .= ")";
	}
$query = "SELECT * FROM webid_auctions WHERE (title LIKE '%".$_POST["term"]."%') " . $catSQL . " AND closed = 0 AND suspended = 0 AND starts <= ".$NOW." AND ends > ".$NOW."";

$result = mysql_query($query);

$array=array();
while ($row = mysql_fetch_object($result)) {
    array_push($array,$row);
}


echo(json_encode($array));
?>
